<?php
class Api{
    function get($req){
        $id = $req->auth["id"];
        $logas = $req->auth["logas"];
        $user = pt\db\Mysql::init()->row("select u.id,u.name,u.creator,r.name as role_name,r.permission as permission
              from user as u left join role as r on r.id = u.role_id where u.id = ? ",$id);
        $menuPath = realpath(__DIR__."/../../../../doc/api/menus.json");
        $menus = json_decode(file_get_contents($menuPath));
        $_row = get_role_permission();
        if($user['creator']){
            $permission = array();
        }else{
            if(!empty($permission = json_decode($user['permission'],1))){
                $permissionDataItems = $_row['permissionDataItems'];
                $_permissionDataItems = array();
                foreach($permissionDataItems as $groupTitle => $tags){
                    foreach($tags as $tagTitle => $items){
                        foreach($items as $key=>$item){
                            $key = $item['url']."|".$item['method'];
                            if(!empty($permission[$key])){
                                $_permissionDataItems[$groupTitle][$tagTitle][$item['title']] = true;
                            }
                        }
                    }
                }
                $permission = $_permissionDataItems;
            }else{
                $permission = array();
            }
        }
        $res = array(
            "appConfig"=>array(
                "mode"=>App::$mode,
                "title"=>"PtApi",
                "menus"=>$menus,
                "deployServerDomain"=>App::$config['deployServerDomain'],
                "deployFile"=>App::$config['deployFile'],
            ),
            "userInfo"=>array(
                "id"=>$user['id'],
                "name"=>$user['name'],
                "creator"=>$user['creator'],
                "logas"=>$logas?true:false,
                "role"=>array(
                    "name"=>$user['role_name'] ? $user['role_name']:"用户",
                    "permission"=>$permission,
                )
            )
        );
        return $res;
    }
}
